<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
import resize from "../../dashboard/mixins/resize";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    xData: {
      type: Array,
      default: () => {
        return ['1d', '2d', '3d', '4d','5d', '6d', '7d', '8d', '9d', '10d', '11d', '12d','13d', '14d']
      }
    },
    kwhData: {
      type: Array,
      default: () => {
        return [79, 52, 200, 334, 390, 330, 34,79, 52, 123, 334, 34, 56, 88]
      }
    },
    coldData: {
      type: Array,
      default: () => {
        return [1709, 152, 1200, 1334, 1390, 1330, 134,179, 152, 1123, 1334, 134, 156]
      }
    },
    copData: {
      type: Array,
      default: () => {
        return [7.9, 5.2, 5.2, 3.5, 3.9, 5.2, 6.2, 5.6, 5.2, 6.8, 5.8, 5.3, 5.6, 5.7]
      }
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  watch: {
    'kwhData': {
      handler() {
        this.$nextTick(() => {
          if (!this.chart) {
            return;
          }
          this.chart.dispose();
          this.chart = null;
          this.initChart();
        });
      }
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.chart.setOption({
        legend: {
          data: ['制冷量','耗电量','COP'],
          textStyle: {
            color: "#ffffff",//文字的颜色。
            fontStyle: "normal",//文字字体的风格。
            fontWeight: "normal",//文字字体的粗细。
            fontSize: 12,
          },
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          }
        },
        grid: {
          top: "30px",
          left: "50px",
          right: "30px",
          bottom: "30px",
        },
        xAxis: [
          {
            type: 'category',
            axisPointer: {
              type: 'shadow'
            },
            lineStyle: {
              color: "#fff",
            },
            axisLabel: {
              show: true,//不显示坐标轴上的文字
              color: "#fff",
            },
            data: this.xData,
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: 'kWh',
            min: 0,
            nameTextStyle: {
              color: "#fff",
              fontSize: 10,
            },
            axisLabel: {
              color: "#fff",
              formatter: function (value, index) {
                if (value >= 1000 && value < 10000) {
                  value = value / 1000 + "k";
                } else if (value >= 10000 && value < 10000000) {
                  value = value / 10000 + "w";
                } else if (value >= 10000000) {
                  value = value / 10000000 + "kw";
                }
                return value;
              }
            },
            splitLine: {
              //分割线配置
              show: false,
              lineStyle: {
                color: "#444",
              },
            },
            axisLine: { //坐标轴线
              show: true,
              lineStyle: {
                color: "#9ca3b1", //最左侧Y轴颜色
              }
            },
            axisTick:{ //坐标轴刻度线
              show:true,
            },
          },
          {
            type: 'value',
            name: 'COP',
            min: 0,
            interval: 5,
            nameTextStyle: {
              color: '#fff',
              fontSize: 10,
            },
            axisLabel: {
              color: "#fff",
            },
            splitLine: {
              //分割线配置
              show: false,
              lineStyle: {
                color: "#444",
              },
            },
            axisLine: { //坐标轴线
              show: true,
              lineStyle: {
                color: "#9ca3b1", //最左侧Y轴颜色
              }
            },
            axisTick:{ //坐标轴刻度线
              show:true,
            },
          }
        ],
        series: [
          {
            name: '制冷量',
            type: 'bar',
            yAxisIndex: 0,
            tooltip: {
              valueFormatter: function (value) {
                return value + ' kWh';
              }
            },
            data:this.coldData,
            animationDuration: 1500,
          },
          {
            name: '耗电量',
            type: 'bar',
            yAxisIndex: 0,
            tooltip: {
              valueFormatter: function (value) {
                return value + ' kWh';
              }
            },
            data: this.kwhData,
            animationDuration: 1500,
          },
          {
            name: 'COP',
            type: 'line',
            yAxisIndex: 1,
            tooltip: {
              valueFormatter: function (value) {
                return value;
              }
            },
            data: this.copData,
            animationDuration: 1500,
          }
        ]
      });
    },
  },
};
</script>

